package org.springrain.selenium.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springrain.frame.controller.BaseController;
import org.springrain.frame.util.Finder;
import org.springrain.frame.util.Page;
import org.springrain.frame.util.ReturnDatas;
import org.springrain.frame.util.property.MessageUtils;
import org.springrain.selenium.entity.Htmlcaseexec;
import org.springrain.selenium.entity.Htmloperation;
import org.springrain.selenium.entity.Htmloperationcollection;
import org.springrain.selenium.entity.Htmlparameter;
import org.springrain.selenium.service.IHtmlSeleniumService;
import org.springrain.selenium.service.IHtmlcaseexecService;

/**
 * TODO 在此加入类描述
 * 
 * @copyright {@link weicms.net}
 * @author springrain<Auto generate>
 * @version 2018-05-21 20:17:39
 * @see org.springrain.selenium.web.Htmloperation
 */
@Controller
@RequestMapping(value = "/system/selenium")
public class HtmlSeleniumController extends BaseController {
	@Resource
	private IHtmlSeleniumService htmlSeleniumService;

	@Resource
	private IHtmlcaseexecService htmlcaseexecService;

	private String listurl = "/selenium/treeList";

	/**
	 * 测试功能的树形界面
	 * 
	 * @param request
	 * @param model
	 * @param htmloperation
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) throws Exception {
		return listurl;
	}

	/**
	 * ajax加载树形结构数据
	 * 
	 * @param request
	 * @param model
	 * @param htmloperation
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/ajax/tree")
	@ResponseBody
	public ReturnDatas listjson(HttpServletRequest request, Model model) throws Exception {
		ReturnDatas returnObject = ReturnDatas.getSuccessReturnDatas();

		Finder finder = Finder.getSelectFinder(Htmloperationcollection.class).append(" order by sortno asc ");
		// ==执行分页查询
		List<Htmloperationcollection> datas = htmlSeleniumService.queryForList(finder, Htmloperationcollection.class);
		returnObject.setData(datas);
		return returnObject;
	}

	/**
	 * 新增/修改 树形结构节点
	 * 
	 */
	@RequestMapping("/ajax/update/node")
	@ResponseBody
	public ReturnDatas updateNode(Model model, Htmloperationcollection htmloperationcollection,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ReturnDatas returnObject = ReturnDatas.getSuccessReturnDatas();
		returnObject.setMessage(MessageUtils.UPDATE_SUCCESS);
		try {

			java.lang.String id = htmloperationcollection.getId();
			if (StringUtils.isBlank(id)) {
				htmloperationcollection.setId(null);
				htmlSeleniumService.saveHtmloperationcollection(htmloperationcollection);
			} else {
				htmlSeleniumService.updateHtmloperationcollection(htmloperationcollection);
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			returnObject.setStatus(ReturnDatas.ERROR);
			returnObject.setMessage(MessageUtils.UPDATE_ERROR);
		}

		returnObject.setData(htmloperationcollection);

		return returnObject;

	}

	/**
	 * 新增/修改 节点的操作数据
	 * 
	 */
	@RequestMapping("/ajax/update/operation")
	@ResponseBody
	public ReturnDatas updateOperation(Model model, Htmloperation htmloperation) throws Exception {
		ReturnDatas returnObject = ReturnDatas.getSuccessReturnDatas();
		returnObject.setMessage(MessageUtils.UPDATE_SUCCESS);
		try {

			Integer parameter = htmloperation.getParameter();

			if (parameter == null) {
				htmloperation.setParameter(0);
			}

			String id = htmloperation.getId();
			if (StringUtils.isBlank(id)) {
				htmloperation.setId(null);
				htmlSeleniumService.saveHtmloperation(htmloperation);
			} else {
				htmlSeleniumService.updateHtmloperation(htmloperation);
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			returnObject.setStatus(ReturnDatas.ERROR);
			returnObject.setMessage(MessageUtils.UPDATE_ERROR);
		}

		returnObject.setData(htmloperation);

		return returnObject;

	}

	/**
	 * 删除树形节点
	 */
	@RequestMapping(value = "/ajax/delete/node")
	@ResponseBody
	public ReturnDatas deleteNode(HttpServletRequest request) throws Exception {

		// 执行删除
		try {
			java.lang.String id = request.getParameter("id");
			if (StringUtils.isNotBlank(id)) {
				htmlSeleniumService.deleteById(id, Htmloperationcollection.class);
				return new ReturnDatas(ReturnDatas.SUCCESS, MessageUtils.DELETE_SUCCESS);
			} else {
				return new ReturnDatas(ReturnDatas.WARNING, MessageUtils.DELETE_WARNING);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return new ReturnDatas(ReturnDatas.WARNING, MessageUtils.DELETE_WARNING);
	}

	/**
	 * 删除 一个操作
	 */
	@RequestMapping(value = "/ajax/delete/operation")
	@ResponseBody
	public ReturnDatas deleteOperation(HttpServletRequest request) throws Exception {

		// 执行删除
		try {
			java.lang.String id = request.getParameter("id");
			if (StringUtils.isNotBlank(id)) {
				htmlSeleniumService.deleteById(id, Htmloperation.class);
				return new ReturnDatas(ReturnDatas.SUCCESS, MessageUtils.DELETE_SUCCESS);
			} else {
				return new ReturnDatas(ReturnDatas.WARNING, MessageUtils.DELETE_WARNING);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return new ReturnDatas(ReturnDatas.WARNING, MessageUtils.DELETE_WARNING);
	}

	/**
	 * ajax加载 操作的 列表
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */

	@RequestMapping("/ajax/operation/list")
	@ResponseBody
	public ReturnDatas listoperationjson(HttpServletRequest request, Model model) throws Exception {
		ReturnDatas returnObject = ReturnDatas.getSuccessReturnDatas();

		Page page = newPage(request, "sortno", "asc");
		page.setPageSize(50);
		String businessId = request.getParameter("businessId");

		if (StringUtils.isBlank(businessId)) {
			return returnObject;
		}
		List<Htmloperation> datas = htmlSeleniumService.findListBybusinessId(businessId, page);
		returnObject.setData(datas);
		returnObject.setPage(page);
		return returnObject;
	}

	/**
	 * 执行测试前检查步骤的变量
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/ajax/exec/pre")
	@ResponseBody
	public ReturnDatas execPre(HttpServletRequest request, Model model) throws Exception {
		ReturnDatas returnObject = ReturnDatas.getSuccessReturnDatas();

		String businessId = request.getParameter("businessId");

		if (StringUtils.isBlank(businessId)) {
			return returnObject;
		}

		List<Htmloperation> list = new ArrayList<>();
		String errorMsg = htmlSeleniumService.updateExecOperationBybusinessId(businessId, list);
		if (StringUtils.isNotBlank(errorMsg)) {
			returnObject = ReturnDatas.getErrorReturnDatas();
			returnObject.setMessage(errorMsg);
		}

		if (CollectionUtils.isEmpty(list)) {
			return returnObject;
		}

		List<Htmlparameter> parameters = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			Htmloperation ho = list.get(i);
			if (1 - ho.getParameter() == 0) {// 需要用户手动录入
				Htmlparameter hp = new Htmlparameter();
				hp.setOperationId(ho.getId());
				hp.setName(ho.getName());
				hp.setSortno(i);
				parameters.add(hp);
			}
		}

		returnObject.setData(parameters);

		return returnObject;
	}

	/**
	 * 执行测试
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/ajax/exec")
	@ResponseBody
	public ReturnDatas exec(HttpServletRequest request, Model model, Htmlcaseexec htmlexec) throws Exception {
		ReturnDatas returnObject = ReturnDatas.getSuccessReturnDatas();

		String businessId = htmlexec.getBusinessId();

		if (StringUtils.isBlank(businessId)) {
			return returnObject;
		}
		String message = htmlcaseexecService.updateExecCaseByBusinessId(htmlexec);
		if (StringUtils.isNotBlank(message)) {
			returnObject = ReturnDatas.getErrorReturnDatas();
			returnObject.setMessage(message);
		}

		return returnObject;
	}

}
